Skip to content

fix(popups): drop disabled criteria from segment criteria array#209

Open
dkoo wants to merge 3 commits into
releasefrom
fix/popups-disabled-criteria-not-nullified
Open

fix(popups): drop disabled criteria from segment criteria array#209
dkoo wants to merge 3 commits into
releasefrom
fix/popups-disabled-criteria-not-nullified

Conversation

@dkoo

@dkoo dkoo commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

All Submissions:

Changes proposed in this Pull Request:

When a number-based segment criterion (e.g. "Articles read in session") is toggled off in the Newspack Campaigns segment editor, the editor still sent a { min: 0, max: 0 } value to the server. Older payloads can also have a bare 0. Those entries were persisted verbatim and localised straight into newspack_popups_view on the front end, so the matching logic continued to evaluate them as active constraints — distorting segment membership and (per the originating report) preventing prompts from being shown to readers who should have qualified.

This change adds a defensive filter in Newspack_Segments_Model that drops criteria whose value carries no constraint — null, empty string, numeric 0, empty array, or a (recursively) all-empty array/object. The filter is applied on both save (so new edits store clean criteria) and read (so existing affected segments are cleaned up automatically without publishers having to re-save every segment). Partially-disabled range criteria (e.g. { min: 3, max: 0 }) are preserved.

Closes NPPM-2890.

How to test the changes in this Pull Request:

  1. On a site with Newspack Campaigns active (the workspace's main site is fine), go to Audience → Campaigns → Segments and create a new segment.
  2. Add the "Articles read in session" criterion and set Min to 3 (or any non-zero value). Save the segment.
  3. Add a Prompt to that segment so it gets emitted to the front end.
  4. Open the front end in a logged-out browser. In the console run newspack_popups_view.segments and find the entry for your segment — confirm the criteria array has the expected { min: 3, max: 0 } (max remains disabled).
  5. Go back to the segment editor, toggle the Min input off on "Articles read in session" (so both Min and Max are disabled) and save.
  6. Reload the front end and run newspack_popups_view.segments again. The articles_read_in_session criterion should no longer appear in the segment's criteria array.
  7. Legacy data cleanup: simulate an already-affected segment by writing a disabled criterion directly to term meta — from the host, run:
    docker exec newspack_dev sh -c "wp term meta update <segment_id> criteria '[{\"criteria_id\":\"articles_read_in_session\",\"value\":{\"min\":0,\"max\":0}},{\"criteria_id\":\"newsletter\",\"value\":\"subscribers\"}]' --format=json --allow-root"
    
    Then visit the front end and confirm newspack_popups_view.segments only shows the newsletter criterion. Also open the segment in the editor and confirm the "Articles read in session" toggle is off (no stale 0).
  8. Run the segment suite to confirm no regressions:
    cd plugins/newspack-popups && n test-php --filter Segments
    

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

The segment editor saves number-based criteria with `{ min: 0, max: 0 }`
when the user toggles them off, and prior payloads can have a bare `0`
value. Those entries were persisted verbatim and localised straight into
`newspack_popups_view`, so the front-end matcher kept evaluating them as
active constraints (NPPM-2890).

Filter empty-valued criteria in `Newspack_Segments_Model` on both save
and read so existing affected segments are cleaned automatically and
future saves stay clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue in Newspack Popups segmentation where disabled criteria (notably number-range criteria saved as { min: 0, max: 0 } or other “empty” values) could still be persisted and then evaluated on the front end, incorrectly affecting segment membership and prompt eligibility.

Changes:

  • Add defensive filtering in Newspack_Segments_Model to remove criteria whose values represent “no constraint” (on both read and save).
  • Apply the filter when reading segments (cleanup of legacy stored data) and when updating segments (prevent new bad data from being stored).
  • Add PHPUnit regression and unit tests covering save-time filtering, read-time legacy cleanup, partial-range preservation, and malformed entry handling.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
plugins/newspack-popups/includes/class-newspack-segments-model.php Filters out “empty/disabled” criteria on segment read and update via filter_criteria() / is_criteria_value_empty().
plugins/newspack-popups/tests/test-segments.php Adds regression/unit tests to ensure disabled/legacy criteria are dropped while valid/partial range criteria are preserved.

Comment thread plugins/newspack-popups/includes/class-newspack-segments-model.php
@dkoo dkoo self-assigned this Jun 3, 2026
dkoo and others added 2 commits June 3, 2026 15:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Address review feedback on PR #209:

- Add `newspack_popups_is_criteria_value_empty` short-circuit and
  `newspack_popups_filter_segment_criteria` final filter so third-party
  criteria registered via `newspack_popups_default_criteria` can opt out
  of the default emptiness check.
- Treat boolean `false` as an empty value (mirrors `is_disabled`
  convention) so future boolean criteria don't accidentally survive.
- Split the int/float zero branches for clarity.
- Tests: cover boolean handling, both extensibility filters, and verify
  the save path writes the filtered form to term meta (not just the
  read path).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@dkoo dkoo marked this pull request as ready for review June 3, 2026 21:31
@dkoo dkoo requested a review from a team as a code owner June 3, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants